<template>
  <div class="login">
    <div class="backgroundBox"></div>
    <div class="loginBox">
      <div class="loginTop"></div>
      <h2 class="loginH2">后台业务管理系统</h2>
      <!-- 登录表单 -->
      <el-form ref="loginFormRef" :model="loginForm" :rules="rules" label-width="0px" class="login_form">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="el-icon-user-solid"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" prefix-icon="el-icon-lock" type="password"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" size="medium" @click="login()">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data: function() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入用户密码', trigger: 'blur' },
          { min: 5, max: 15, message: '长度在 5 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetForm() {
      this.$refs.loginFormRef.resetFields()
    },
    login() {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) {
          return
        }
        const { data: res } = await this.$http.post('login', this.loginForm)
        console.log(res)
        if (res.status !== 200) {
          this.$message.error('登录失败！')
        } else {
          this.$message.success('登录成功')
          // 将登录成功后的token存储到sessionStorage 中
          window.sessionStorage.setItem('token', res.data.token)
          window.sessionStorage.setItem('username', res.data.body.username)
          // 通过编程式导航跳转到页面主页
          this.$router.push('/home')
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.login {
  .backgroundBox {
    position: fixed;
    top: 50%;
    transform: translateY(-50%);
    width: 100%;
    height: 250px;
    background-color: #20a5ff;
  }
  .loginBox {
    border-width: 0px;
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    height: 420px;
    transform: translate(-50%, -50%);
    background: inherit;
    background-color: rgba(255, 255, 255, 1);
    border: none;
    border-left: 0px;
    border-right: 0px;
    border-bottom: 0px;
    border-radius: 0px;
    border-top-left-radius: 0px;
    border-top-right-radius: 0px;
    border-bottom-right-radius: 0px;
    border-bottom-left-radius: 0px;
    -moz-box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.349019607843137);
    -webkit-box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.349019607843137);
    box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.349019607843137);
    font-family: '微软雅黑 Bold', '微软雅黑 Regular', '微软雅黑';
    font-weight: 700;
    font-style: normal;
    font-size: 16px;
    color: #999999;
    text-align: left;
    line-height: 20px;
    .loginTop {
      width: 100%;
      height: 10px;
      background-color: #20a5ff;
    }
    .loginH2 {
      margin-top: 100px;
      color: #20a5ff;
      text-align: center;
    }
  }
  .el-form-item {
    box-sizing: border-box;
    padding: 0 20px;
  }
  .el-button {
    width: 100%;
    height: 45px;
    box-sizing: border-box;
    padding: 0 20px;
  }
}
</style>
